package two_xia.myPainter.utils;

import two_xia.myPainter.shape.Shape;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class FileUtils {

    private static final long serialVersionUID = 1L;
    /**
     * 图形形状的标记
     */
    public static int index = 0;
    /**
     * 图形存储单元
     */
    public static Shape[] itemList = new Shape[5000];
    /**
     * 保存文件的标志
     */
    private static int saved = 0;
    private static String fontName = " 宋体 ";
    private static int fSize = 16;
    /**
     * 粗体,默认正常
     */
    private static int blodtype = Font.PLAIN;
    /**
     * 斜体
     */
    private static int italic = Font.PLAIN;
    /**
     * 初始状态是画笔
     */
    private static int currentChoice = 3;
    /**
     * 铅笔或橡皮擦图形的存储长度
     */
    private int lengthCount;

    //文件保存
    public static void saveFile(JPanel drawingArea) {
        FileUtils FileUtils = new FileUtils();
        // 文件选择器
        JFileChooser fileChooser = FileUtils.getjFileChooser();
        // 弹出一个 "Save File" 文件选择器对话框
        int result = fileChooser.showSaveDialog(drawingArea);
        if (result == JFileChooser.CANCEL_OPTION) {
            return;
        }
        File fileName = fileChooser.getSelectedFile();

        if (!fileName.getName().endsWith(fileChooser.getFileFilter().getDescription())) {
            String t = fileName.getPath() + fileChooser.getFileFilter().getDescription();
            fileName = new File(t);
        }
        fileName.canWrite();
        if ("".equals(fileName.getName())) {
            JOptionPane.showMessageDialog(fileChooser, "无效的文件名", "无效的文件名", JOptionPane.ERROR_MESSAGE);
        }

        BufferedImage image = FileUtils.createImage(drawingArea);
        try {
            ImageIO.write(image, "png", fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    // 打开文件
    public static void openFile(JPanel drawingArea) {
        FileUtils fileUtils = new FileUtils();
        JFileChooser fileChooser = fileUtils.getjFileChooser();
        // 弹出一个 "Open File" 文件选择器对话框
        int result = fileChooser.showOpenDialog(drawingArea);
        if (result == JFileChooser.CANCEL_OPTION) {
            return;
        }
        // 得到选择文件的名字
        File fileName = fileChooser.getSelectedFile();
        if (!fileName.getName().endsWith(fileChooser.getFileFilter().getDescription())) {
            JOptionPane.showMessageDialog(drawingArea, "文件格式错误！");
            return;
        }
        fileName.canRead();

        if ("".equals(fileName.getName())) {
            JOptionPane.showMessageDialog(fileChooser, "无效的文件名", "无效的文件名", JOptionPane.ERROR_MESSAGE);
        }
    }


    private JFileChooser getjFileChooser() {
        // 文件选择器
        JFileChooser fileChooser = new JFileChooser();
        // 设置文件显示类型为仅显示文件
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        // 文件过滤器
        JpgFilter jpg = new JpgFilter();
        BmpFilter bmp = new BmpFilter();
        PngFilter png = new PngFilter();
        GifFilter gif = new GifFilter();
        // 向用户可选择的文件过滤器列表添加一个过滤器。
        fileChooser.addChoosableFileFilter(jpg);
        fileChooser.addChoosableFileFilter(bmp);
        fileChooser.addChoosableFileFilter(png);
        fileChooser.addChoosableFileFilter(gif);
        // 返回当前的文本过滤器，并设置成当前的选择
        fileChooser.setFileFilter(fileChooser.getFileFilter());
        return fileChooser;
    }


    // 创建image，由saveFile方法调用
    // 将画板内容画到panelImage上
    BufferedImage createImage(JPanel panel) {

        int width = panel.getWidth();
        int height = panel.getHeight();
        BufferedImage panelImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = panelImage.createGraphics();

        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);
        graphics.translate(0, 0);
        panel.paint(graphics);
        graphics.dispose();
        return panelImage;
    }


    // 文件过滤
    class JpgFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            return f.getName().endsWith(".jpg");
        }

        @Override
        public String getDescription() {
            return ".jpg";
        }

    }

    class BmpFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            return f.getName().endsWith(".bmp");
        }

        @Override
        public String getDescription() {
            return ".bmp";
        }

    }

    class GifFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            return f.getName().endsWith(".gif");
        }

        @Override
        public String getDescription() {
            return ".gif";
        }

    }

    class PngFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            return f.getName().endsWith(".png");
        }

        @Override
        public String getDescription() {
            return ".png";
        }

    }
}
